﻿using ConfExpert.Datasets;
using ConfExpert.Utility;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;


using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class FoyFarkliEtiket : XPopupForm
    {       
        int idx;
        public FoyFarkliEtiket(int idx)
        {
            this.idx = idx;
            InitializeComponent();
        }        
        DataTable dt = null;
        private void BedenDagilimi_Load(object sender, EventArgs e)
        {
            
            this.Text = "Beden Dağılımı";

            txtInfo.Caption = idx + " | " + "Yabancı etiket dikilecek adetleri girin";

            dt = ExecuteDataTable("GetKaufYabanciEtiket", idx);

            MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(dt.Rows[0]["DagilimId"].ToInt());

            if (dt.Rows.Count==1)
            {
                dt.Rows.Add(dt.NewRow());
            }

            gv.Init(dt);

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                gv.Columns[i].Visible = false;
            }

            if (r != null)
            {
                for (int i = 1; i < 29; i++)
                {
                    string fieldName = String.Format("A{0:00}", i);
                    string cap = r[String.Format("A{0:00}", i)].ToString();
                    if (cap != "")
                    {
                        gv.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                        gv.Columns[fieldName].VisibleIndex = i;
                    }
                }
            }

            gv.Columns["CountryId"].VisibleIndex = 0;

            gv.OptionsView.ShowAutoFilterRow = false;
            gv.Appearance.FocusedCell.BackColor = Color.YellowGreen;
            gv.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            gv.BestFitColumns();
            btnSave.ItemClick += btnSave_ItemClick;
        }

        void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();
            for (int i = 1; i < dt.Rows.Count; i++)
            {
                int yabanciDil = dt.Rows[i]["CountryId"].ToInt();
                if (yabanciDil == 0)
                {
                    ShowInfoMessage("Lütfen dikilecek etiketin ülkesini girin.");
                    return;
                }
            }

            foreach (GridColumn col in gv.VisibleColumns)
            {
                //for (int i = 1; i < dt.Rows.Count; i++)
                //{
                if (col.FieldName != "CountryId")
                {
                    int val0 = dt.Rows[0][col.FieldName].ToInt();
                    //int val1 = dt.Rows[i][col.FieldName].ToInt();
                    int val1 = dt.Compute("Sum(" + col.FieldName + ")", "CountryId is not null").ToInt();
                    if (val1 > val0)
                    {
                        ShowInfoMessage("Ana satırdan fazla adet giremezsiniz.");
                        return;
                    }
                }
                //}                
            }

            string xml = dt.Serialize();

            var yabanciEtiketId = ExecuteScalar("InsKaufYabanciEtiket", xml);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void gv_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle == 0)
            {
                gv.FocusedRowHandle = 1;
            }          
        }

        public string UpdateString = "";



    }
}
